package com.spider.ask120.service;

import java.util.ArrayList;
import java.util.List;

import org.jdiy.core.Args;
import org.jdiy.core.Dao;
import org.jdiy.core.Ls;
import org.jdiy.core.Rs;

import com.spider.utils.PinyinUtil;

public class ChannelService extends BaseService{
	/**
	 * 保存渠道
	 * @param str_id
	 * @param name
	 * @param parent_id
	 * @return
	 */
	public static Rs saveChannel(String str_id, String name, String parent_id){
		String pinyin = PinyinUtil.converterToFirstSpell(name); 
		Rs ch = dao.rs(new Args("channel", "str_id='"+str_id+"' and name ='"+name+"' "
				+ "and pinyin='"+pinyin+"' and parent_id="+parent_id)); 
		ch.set("str_id", str_id);
		ch.set("name", name);
		ch.set("pinyin", pinyin);
		ch.set("parent_id", parent_id);
		dao.save(ch);

		return ch;
	}

	/**
	 * 查询“健康经验”频道中的最底层栏目
	 * @return
	 */
	public static List<Rs> getChannelLowCatalog(){
		List<Rs> ids = new ArrayList<Rs>();
		Rs topChannel = dao.rs(new Args("channel", "str_id='jingyan' ")); 
		getChild(dao, topChannel, ids);

		return ids;
	} 

	private static void getChild(Dao dao, Rs parent, List<Rs> ids){
		Ls child = dao.ls(new Args("channel", " parent_id = "+parent.get("id") ));
		if(child.getItems().length > 0){
			for(Rs channel : child.getItems()){
				getChild(dao, channel, ids);
			}
		}else{
			ids.add(parent);
		}
	}

	/**
	 * 查询“健康经验”频道中的底层栏目
	 * @return
	 */
	public static List<Rs> getChannelByParentId(Integer parent_id){
		List<Rs> ids = new ArrayList<Rs>();
		Rs topChannel = dao.rs(new Args("channel", "parent_id="+parent_id+" ")); 
		getChild(dao, topChannel, ids);

		return ids;
	}

	/**
	 * 根据str_id查询频道
	 * @return
	 */
	public static Rs getChannelByStrId(String str_id){
		Rs ch = dao.rs(new Args("channel","str_id='"+str_id+"' AND parent_id=1"));
		return ch;
	}
	/**
	 * 根据频道id查询频道信息
	 * @param id
	 * @return
	 */
	public static Rs getChannelById(String id){
		Rs ch = dao.rs(new Args("channel","id="+id));
		return ch;
	}
	/**
	 * 保存频道
	 * @param cha
	 */
	public static void save(Rs cha) {
		dao.save(cha);
	}
	/**
	 * 根据频道名查询频道
	 * @param name
	 * @return
	 */
	public static Rs getCHannelByName(String name) {
		Rs ch = dao.rs(new Args("channel","name='"+name+"' "));
		return ch;
	}
	/**
	 * 根据str_id和parents_id查询频道
	 * @return
	 */
	public static Rs getChannelByStrIdAndPid(String str_id,String channel_id){
		Rs ch = dao.rs(new Args("channel","str_id='"+str_id+"' AND parent_id="+channel_id));
		return ch;
	}
	public static void main(String[] args) {
		List<Rs> channels = ChannelService.getChannelLowCatalog();
		//System.out.println(channels.size());
		for(Rs channel : channels){
			String id = channel.get("id");
			String str_id = channel.get("str_id");
			//System.out.println(id +" ----- " + str_id);
			channel.set("is_bottom", 1);
			dao.save(channel);
		}
//		List<Rs> aa = getChannelLowCatalog();
//		for(Rs a : aa){
//			//System.out.println(a.get("id") +" " + a.get("name"));
//		}
	}
	
}
